#!/bin/bash
#
# Compression / decompression routines for Pulley
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 17 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## pcomp_lazy_load <method>
##
## Loads the functions that implement a compression <method>, by sourcing the
## file that implements the requested method, if the functions are not
## already loaded. Returns 0 upon success, non-zero if the compression method
## cannot be found or a load error occurs.
##
function pcomp_lazy_load() {
	lazy_load pcomp_supported_$1 "compression" "$1.method"
	return $?
}


## pcomp_supported <method>
##
## Tests to determine whether or not a particular compression <method> is
## supported on the current system. Returns 0 if the method is supported,
## non-zero if the method is unsupported or unavailable on the system.
function pcomp_supported() {
	local status=0
	
	pcomp_lazy_load $1
	status=$?
	
	if ((status == 0)); then
		pcomp_supported_$1
		status=$?
	fi
	
	return ${status}
}


## pcomp_compress <method> <input file> <output file>
##
## Compresses <input file> and saves to <output file>, using the specified
## compression <method>. Returns 0 if successful. If the requested
## compression method is unavailable, or an error occurs, returns non-zero.
##
function pcomp_compress() {
	local status=1
	
	if pcomp_supported $1; then
		pcomp_compress_$1 "$2" "$3"
		status=$?
	fi
	
	return ${status}
}


## pcomp_decompress <method> <input file> <output file>
##
## Decompresses <input file> and saves to <output file>, using the requested
## compression <method>. Returns 0 if successful, non-zero if the compression
## method is unavailable or an error occurs.
##
function pcomp_decompress() {
	local status=1
	
	if pcomp_supported $1; then
		pcomp_decompress_$1 "$2" "$3"
		status=$?
	fi
	
	return ${status}
}

